﻿namespace BadHabit.Factory.Reports
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    public class ProjectPerformanceReport : Report
    {
        private Data.ProjectPerformanceDataSetTableAdapters.ProjectPerformanceTableAdapter performanceTA = new BadHabit.Factory.Data.ProjectPerformanceDataSetTableAdapters.ProjectPerformanceTableAdapter();
        private Data.ProjectPerformanceDataSetTableAdapters.ProjectRoleTableAdapter performanceRoleTA = new BadHabit.Factory.Data.ProjectPerformanceDataSetTableAdapters.ProjectRoleTableAdapter();

        private int year;
        private string member = null;

        public ProjectPerformanceReport(FactoryLogic factory, int year, string member)
            : base(factory)
        {
            this.year = year;
            this.member = member;
        }

        public ProjectPerformanceReport(FactoryLogic factory, int year)
            : base(factory)
        {
            this.year = year;
        }

        /// <summary>
        /// The template type name.
        /// </summary>
        public override string TemplateTypeName
        {
            get
            {
                return "ProjectPerformanceReport";
            }
        }

        /// <summary>
        /// Adjusts the dataset for XPS render.
        /// </summary>
        /// <param name="projectUId">The projectUId</param>
        /// <returns>The adjusted dataset</returns>
        public override System.Data.DataSet CreateDataSet()
        {
            Data.ProjectPerformanceDataSet ds = new BadHabit.Factory.Data.ProjectPerformanceDataSet();
            if (this.member == null)
            {
                this.performanceTA.FillByYear(ds.ProjectPerformance, this.year);
            }
            else
            {
                this.performanceTA.FillByYearAndMember(ds.ProjectPerformance, this.year, this.member);
            }

            this.performanceRoleTA.FillByYear(ds.ProjectRole, this.year);

            return ds;
        }

        public override System.Data.DataSet AdjustDataSet(System.Data.DataSet report)
        {
            report = base.AdjustDataSet(report);
            report.Tables["General"].Columns.Add("SelectedYear", typeof(int));
            report.Tables["General"].Columns.Add("SelectedMember", typeof(string));
            report.Tables["General"].Columns.Add("SelectedMemberName", typeof(string));
            report.Tables["General"].Rows[0]["SelectedYear"] = this.year;
            
            if (this.member != null)
            {
                report.Tables["General"].Rows[0]["SelectedMember"] = this.member;
                Data.TeamDataSet.MemberRow memberRow = factory.FindTeamMember(this.member);
                if (memberRow != null)
                {
                    report.Tables["General"].Rows[0]["SelectedMemberName"] = memberRow.NickName;
                }
            }

            report.Tables["ProjectPerformance"].Columns.Add("StartDateString", typeof(string));
            report.Tables["ProjectPerformance"].Columns.Add("EndDateString", typeof(string));
            report.Tables["ProjectPerformance"].Columns.Add("EstimatedEndDateString", typeof(string));
            foreach (Data.ProjectPerformanceDataSet.ProjectPerformanceRow p in report.Tables["ProjectPerformance"].Rows)
            {
                if (!p.IsStartDateNull())
                {
                    p["StartDateString"] = p.StartDate.ToShortDateString();
                }

                if (!p.IsEndDateNull())
                {
                    p["EndDateString"] = p.EndDate.ToShortDateString();
                }

                p["EstimatedEndDateString"] = p.EstimatedEndDate.ToShortDateString();
            }

            return report;
        }
    }
}
